﻿#include "SDLRenderAVFrameWindow.h"
#include <QFileDialog>


SDLRenderAVFrameWindow::SDLRenderAVFrameWindow(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	this->setWindowTitle("SDL渲染AVFrame");
	setAttribute(Qt::WA_DeleteOnClose, true);  // 关闭时自动销毁窗口
}

void SDLRenderAVFrameWindow::onSelectPixFormatIndex1(int pos)
{
	switch (pos)
	{
	case 0:
		m_fmt1 = IVideoRenderView::YUV420P;
		break;
	case 1:
		m_fmt1 = IVideoRenderView::RGB24;
		break;
	case 2:
		m_fmt1 = IVideoRenderView::RGBA;
		break;
	case 3:
		m_fmt1 = IVideoRenderView::ARGB;
		break;
	}

}

void SDLRenderAVFrameWindow::onSelectPixFormatIndex2(int pos)
{
	switch (pos)
	{
	case 0:
		m_fmt2 = IVideoRenderView::YUV420P;
		break;
	case 1:
		m_fmt2 = IVideoRenderView::RGB24;
		break;
	case 2:
		m_fmt2 = IVideoRenderView::RGBA;
		break;
	case 3:
		m_fmt2 = IVideoRenderView::ARGB;
		break;
	}

}

void SDLRenderAVFrameWindow::selectVideo1Func()
{
	QString filter = "RGB 文件 (*.*);;所有文件 (*.*)";
	// 打开文件选择对话框，选择单个文件
	QString filePath = QFileDialog::getOpenFileName(this, "选择 RGB 文件", "", filter);
	if (filePath.isEmpty()) {
		return;
	}
	if (playController1)
	{
		delete playController1;
		playController1 = nullptr;
	}
	playController1 = MultipleVideosPlayController::Create();
	playController1->Close();
	playController1->mWidth = ui.video1Width->text().toInt();
	playController1->mHeight = ui.video1Height->text().toInt();
	playController1->mPixFormat = m_fmt1;
	playController1->win_id = (void*)ui.labelVideo1->winId();
	playController1->fps = ui.editFps1->text().toInt();
	playController1->mFilePath = filePath;
	playController1->Init();
	playController1->callback = [this](int fps) {
		qDebug() << "执行了回调函数1";
		ui.labelFps1->setText(QString::number(fps));
		};
	playController1->Start();
	//playController1->Draw();
}



void SDLRenderAVFrameWindow::selectVideo2Func()
{
	QString filter = "RGB 文件 (*.*);;所有文件 (*.*)";
	// 打开文件选择对话框，选择单个文件
	QString filePath = QFileDialog::getOpenFileName(this, "选择 RGB 文件", "", filter);
	if (filePath.isEmpty()) {
		return;
	}
	if (playController2)
	{
		delete playController2;
		playController2 = nullptr;
	}
	playController2 = MultipleVideosPlayController::Create();
	playController2->Close();
	playController2->mWidth = ui.video2Width->text().toInt();
	playController2->mHeight = ui.video2Height->text().toInt();
	playController2->mPixFormat = m_fmt2;
	playController2->win_id = (void*)ui.labelVideo2->winId();
	playController2->mFilePath = filePath;
	playController2->fps = ui.editFps2->text().toInt();
	playController2->Init();
	playController2->callback = [this](int fps) {
		qDebug() << "执行了回调函数2";
		ui.labelFps2->setText(QString::number(fps));
		};
	playController2->Start();
	//playController2->Draw();
}

void SDLRenderAVFrameWindow::closeEvent(QCloseEvent* event) {
	event->accept();  // 接受关闭事件（允许关闭）
	if (playController1)
	{
		playController1->Close();
	}
	if (playController2)
	{
		playController2->Close();
	}
	qDebug() << "窗口已关闭";
}

void SDLRenderAVFrameWindow::resizeEvent(QResizeEvent* event) {
	// 调用父类实现（保留默认行为）
	QWidget::resizeEvent(event);
}

SDLRenderAVFrameWindow::~SDLRenderAVFrameWindow()
{
	if (playController1)
	{
		delete playController1;
		playController1 = nullptr;
	}
	if (playController2)
	{
		delete playController2;
		playController2 = nullptr;
	}

}
